package com.android.friend.lib.network.httptask;

import java.io.IOException;
import java.net.SocketTimeoutException;

import org.apache.http.conn.ConnectTimeoutException;

import android.content.Context;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;

import com.android.friend.app.R;
import com.android.friend.lib.dialog.util.DialogUtil;
import com.android.friend.lib.network.util.NetworkUtility;

/**
 * @param <Params>
 * @param <Progress>
 * @param <Result>
 */
public abstract class MobiNetworkTask<Params, Progress, Result> extends BaseTask<Params, Progress, Result> {
    /**
     * @param pContext
     * @param pShowLoading
     */
    private Handler mHandler;
    private int TIME_OUT =1;
    public MobiNetworkTask(final Context pContext, boolean pShowLoading) {
        super(pContext, pShowLoading);
        mHandler = new Handler(new Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if(mDialog!= null && mDialog.isShowing()) {
                    mDialog.dismiss();
                }
                DialogUtil.showConfirmDialog(pContext, "Message", "Connection Error");
                return false;
            }
        });
    }

    @Override
    protected final Result doInBackground(Params... params) {
        Result result = null;
        try {
            result = this.processInBackground(params);
        } catch (SocketTimeoutException e) {
            e.printStackTrace();
            mHandler.sendEmptyMessage(TIME_OUT);
            onSocketTimeout(e, mContext.get(), params);
        } catch (ConnectTimeoutException e) {
            e.printStackTrace();
            mHandler.sendEmptyMessage(TIME_OUT);
            onConnectionTimeout(e, mContext.get(), params);
        } catch (IOException e) {
            e.printStackTrace();
            onIOException(e, mContext.get(), params);
        }
        return result;
    }

    @Override
    protected final void onPostExecute(Result result) {
        this.onUIUpDate(result);
        super.onPostExecute(result);
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * com.smartebook.android.mobibook.networkhandler.networktask.BaseTask#run(android.content.Context
     * , Params[])
     */
    @Override
    public final void run(Params... params) {
        if (this.checkNetwork()) {
            super.run(params);
        } else {
            onNoNetworkConnected(mContext.get(), params);
        }
    }

    protected boolean checkNetwork() {
        return NetworkUtility.isInternetConnected(mContext.get());
    }

    public abstract Result processInBackground(Params... pParams) throws IOException;

    public abstract void onUIUpDate(Result result);

    protected void onNoNetworkConnected(Context pContext, Params... params) {};

    protected void onConnectionTimeout(IOException exception, Context pContext, Params... params) {};

    protected void onSocketTimeout(IOException exception, Context pContext, Params... params) {};

    protected void onIOException(IOException exception, Context pContext, Params... params) {};

}
